/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package varredor.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import varredor.StartPoint;
import varredor.controller.ArquivoCompartilhadoController;
import varredor.model.Registro;
import varredor.web.RecebeuMensagemListener;

/**
 *
 * @author Murilo
 */
public class RegistroDialog extends javax.swing.JDialog {

	/**
	 * Creates new form RegistroDialog
	 */
	public RegistroDialog(java.awt.Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();

		carregarRegistros();
		setLocationRelativeTo(parent);
		setTitle("Adicionar Registro.");
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of
	 * this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jList1 = new javax.swing.JList();
		jTextField1 = new javax.swing.JTextField();
		jLabel1 = new javax.swing.JLabel();
		adicionarButton = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jScrollPane1.setViewportView(jList1);

		jLabel1.setText("Valor da Linha");

		adicionarButton.setText("Adicionar");
		adicionarButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				adicionarButtonActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			layout.createSequentialGroup().addGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
					layout.createSequentialGroup().addGap(22, 22, 22).addGroup(
						layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel1).addComponent(jTextField1,
							javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(adicionarButton))
						.addGap(0, 0, Short.MAX_VALUE)).addGroup(
					javax.swing.GroupLayout.Alignment.TRAILING,
					layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 403,
						javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
			javax.swing.GroupLayout.Alignment.TRAILING,
			layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addGap(6, 6, 6).addComponent(jTextField1,
				javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(adicionarButton).addGap(38, 38, 38).addComponent(
					jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
					javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(81, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void adicionarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarButtonActionPerformed
		adicionarRegistro();
	}//GEN-LAST:event_adicionarButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton adicionarButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JList jList1;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextField jTextField1;

	// End of variables declaration//GEN-END:variables

	private void adicionarRegistro() {

		String valor = jTextField1.getText();

		if (valor != null && !valor.trim().isEmpty()) {
			Registro registro = new Registro();

			registro.setData(new Date());
			registro.setValor(valor);

			registro.setMac(StartPoint.getINSTANCE().getMAC());
			registro.setIp(StartPoint.getINSTANCE().getIP());

			ArquivoCompartilhadoController.getINSTANCE().adicionarRegistro(registro);

			jTextField1.setText("");
			carregarRegistros();

			dispararAtualizador();
		}
	}

	private void dispararAtualizador() {
		try {
			StartPoint.getINSTANCE().getConexaoBroad().envia(RecebeuMensagemListener.MSG_ATUALIZA);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void carregarRegistros() {

		ArrayList<Registro> registros = new ArrayList<Registro>(ArquivoCompartilhadoController.getINSTANCE().retornarRegistros());

		Collections.sort(registros);

		jList1.setListData(registros.toArray());

	}
}
